package com.example.discover.view

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.discover.viewmodel.BookViewportViewModel
import com.example.featuresdk.core.AbstractFeatureSDK
import com.example.featuresdk.view.AppBottomNavigationView
import com.example.featuresdk.view.AppHeaderFeatureView
import com.example.featuresdk.view.TopicEntryView
import com.example.featuresdk.viewmodel.NavigationViewModel
import java.lang.Integer.max


@ExperimentalFoundationApi
@ExperimentalUnitApi
@Composable
fun DiscoverListView(
    navigationViewModel: NavigationViewModel,
    featureSDK: AbstractFeatureSDK, bookViewportViewModel: BookViewportViewModel = viewModel()) {

    Column(modifier = Modifier
        .background(Color.Transparent).fillMaxSize()) {

        AppHeaderFeatureView(navigationViewModel)

        //https://developer.android.google.cn/jetpack/compose/lists
        LazyColumn(modifier = Modifier
            .background(Color.Transparent)
            .fillMaxWidth()
            .height(navigationViewModel.appBodyHeightDp.dp),
            contentPadding = PaddingValues(horizontal = 0.dp,vertical = 8.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp)){

            bookViewportViewModel.groupedTopics["V"]?.let {
                item{
                    TopicEntryView(it.get(0)!!,navigationViewModel,true)
                }

            }

            var maxCount = 5
            bookViewportViewModel.groupedTopics.forEach { (key, topics) ->
                if("V" != key && maxCount-- > 0) {
                    stickyHeader(key) {
                        Text(text = "", modifier = Modifier
                            .fillMaxWidth()
                            .height(10.dp)
                            .background(Color.LightGray),
                            color = Color.Gray,
                            fontSize = TextUnit(4f, TextUnitType.Em), textAlign = TextAlign.Start)
                    }
                    items(topics.subList(0,max(topics.size - 1,1))) { topic ->
                        TopicEntryView(topic, navigationViewModel, true)
                    }
                }
            }
        }

        AppBottomNavigationView(navigationViewModel)
    }
//        OutlinedButton(onClick = {
//            navController.navigate("Payment"){
//                launchSingleTop = true
//            }
//            selectedTabIndex.value = 1
//        }) {
//            Text(text = "Click to navigate to Payment screen")
//        }
}
